package com.intexh.quickandroid.module.chat.ui.face;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by roger on 4/25/14.
 */
public class EmojiPageAdapter extends PagerAdapter implements AdapterView.OnItemClickListener {
    private Context mContext;
    private List<GridView> pages = new ArrayList<GridView>();
    private EmojiClickListener emojiClickListener;
    private int verticalSpacing = 16;
    public final static String DELETE_CODE = "delete_back";

    private final int row = 4;
    private final int colum = 7;

    public int getRow() {
        return row;
    }

    public void setVerticalSpacing(int verticalSpacing) {
        if (this.verticalSpacing != verticalSpacing) {
            this.verticalSpacing = verticalSpacing;
            for (GridView gridView : pages) {
                gridView.setVerticalSpacing(this.verticalSpacing);
            }

            notifyDataSetChanged();
        }
    }

    public EmojiPageAdapter(Context context, EmojiClickListener clickListener) {
        this.mContext = context;
        this.emojiClickListener = clickListener;
        initPages();
    }

    public void initPages() {
        int itemCount = colum * row - 1;
        int pageCount = (int) Math.ceil(EmojiFactory.codePoints.length / ((float) itemCount));
        pages.clear();
        for (int i = 0; i < pageCount; i++) {
          try {
        	  GridView gridView = new GridView(mContext);
              gridView.setGravity(Gravity.CENTER);
              gridView.setNumColumns(colum);
              gridView.setHorizontalSpacing(10);
              gridView.setVerticalSpacing(this.verticalSpacing);
              gridView.setSelector(android.R.color.transparent);
              EmojiAdapter adapter = new EmojiAdapter(mContext, i * itemCount, itemCount);
              gridView.setAdapter(adapter);
              gridView.setOnItemClickListener(this);
              pages.add(gridView);
		} catch (OutOfMemoryError e) {
			 System.gc();
		}catch (Exception e) {


		}
        	
        }
    }

    @Override
    public int getCount() {
        return pages.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        container.addView(pages.get(position));
        return pages.get(position);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(pages.get(position));
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        if (emojiClickListener != null) {
            emojiClickListener.onEmojiClick((String) adapterView.getAdapter().getItem(i));
        }
    }

    public void setEmojiClickListener(EmojiClickListener emojiClickListener) {
        this.emojiClickListener = emojiClickListener;
    }

    public interface EmojiClickListener {
        public void onEmojiClick(String emojiCode);
    }
}
